package client.comm.baoding.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.comm.baoding.api.bean.*
import client.comm.baoding.ui.viewmodel.MiddleVm
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.commlib.utils.StoreUtil

class TradeDetailModel(app: Application) : MiddleVm(app) {

    val isShowingDialog = MutableLiveData<Boolean>(false)
    val isShowing = MutableLiveData<Boolean>(false)
    val isOneLoading = MutableLiveData<Boolean>(false)
    val pageOne = MutableLiveData<Int>(0)

    val goodsDetail = MutableLiveData<JsonResult<GoodsDetail>>()
    val tradeGoodsDetail = MutableLiveData<JsonResult<GoodsDetailTrade>>()
    val callUser = MutableLiveData<JsonResult<UserInfo>>()

    val buyRet = MutableLiveData<JsonResult<Ret>>()
    val tradeRet = MutableLiveData<JsonResult<Ret>>()
    val revokeRet = MutableLiveData<JsonResult<Ret>>()
    val tradePickRet = MutableLiveData<JsonResult<Ret>>()

    val takeRet = MutableLiveData<JsonResult<Ret>>()

    fun userInfo() {
        hold({
            server.userinfo().apply {
                body()?.let {
                    if (it.isSuccess) {
                        StoreUtil.saveBean(getApplication(), it.data!!)
                    }
                }
            }
        }, success = {
            callUser.value = it
        })
    }

    fun holdTradeCall(id: String) {
        hold({
            server.trade_detail(id, "")
        }, {
        }, success = {
            goodsDetail.value = it
        }, complete = {
        })
    }

    fun holdGoodsDetailCall(id: String) {
        hold({
            server.trade_goods_detail(id)
        }, {
        }, success = {
            tradeGoodsDetail.value = it
        }, complete = {
        })
    }

//    fun holdBuyYj(detail: TradeGoodsDetail, num: String) {
//        hold({
//            detail.goods_info?.let {
//                server.trade_buy_yj(
//                    "${it.goods_id}",
////                    "${it.price}",
//                    num
//                )
//            }
//
//        }, {
//            isShowing.value = true
//        }, success = {
//            buyRet.value = it
//        }, complete = {
//            isShowing.value = false
//
//        })
//    }

    fun holdBuyTj(detail: GoodsDetailTrade, num: String) {
        hold({
            server.trade_buy_tj(
                "${detail.goods_info.goods_id}",
//                    "${it.price_pf}",
                num
            )

        }, {
            isShowing.value = true
        }, success = {
            tradeRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

    fun holdTradeHangOrder(detail: GoodsDetailTrade, num: String) {
        hold({
            server.trade_hang_order(
                "${detail.goods_info.goods_id}",
//                    "${it.price_pf}",
                num
            )
        }, {
            isShowing.value = true
        }, success = {
            tradeRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }


    fun holdTradePick(order_id: String?, goods_id: String, addressId: String, num: String) {

    }

    fun tradeRevokeOrder(b: HangOrderRecord.Order) {
        if (isOneLoading.value!!)
            return
        hold({
            server.trade_revoke_order("${b.id}")
        }, {
            isOneLoading.value = true
        }, {
            revokeRet.value = it
        }, complete = {
            isOneLoading.value = false
        })
    }

    fun add_shopping_cart(goodsId: String, num: String) {
        hold({
            server.add_shopping_cart(
                goodsId,
                num,
            )
        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun holdTiHuo(goods_id: String, addressId: String, num: String) {
        hold({
            server.trade_take_tj(
                "${goods_id}",
                addressId,
                num,
            )
        }, {
            isShowing.value = true
        }, success = {
            takeRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

//    fun holdBuyTh(detail: TradeGoodsDetail, num: String) {
//        hold({
//            detail.goods_info?.let {
//                server.trade_take(
//                    "${it.goods_id}",
////                    "${it.price_pf}",
//                    num
//                )
//            }
//
//        }, {
//            isShowing.value = true
//        }, success = {
//            buyRet.value = it
//        }, complete = {
//            isShowing.value = false
//
//        })
//    }

//    fun holdBuyYh(detail: TradeGoodsDetail, num: String) {
//        hold({
//            detail.goods_dict?.let {
//                server.trade_goods_buy_yh(
//                    "${it.goods_id}",
//                    "${it.price_pf}",
//                    num
//                )
//            }
//
//        }, {
//            isShowing.value = true
//        }, success = {
//            buyRet.value = it
//        }, complete = {
//            isShowing.value = false
//
//        })
//    }

}